<?php

/**
 * 跑腿设置
 */

namespace app\admin\controller;
use think\facade\View;
use think\facade\Lang;
use think\Validate;
/**
 * ============================================================================
 * DSO2O多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 控制器
 */
class  O2oErrand extends AdminControl {

    public function initialize() {
        parent::initialize(); // TODO: Change the autogenerated stub
        Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/o2o_errand.lang.php');
    }
    public function other() {
        $config_model = model('config');
        if (!request()->isPost()) {
            $list_config = rkcache('config', true);
            View::assign('list_config', $list_config);
            $this->setAdminCurItem('other');
            return View::fetch();
        } else {
            $update_array=array();
            $update_array['o2o_errand_deposit'] = abs(floatval(input('param.o2o_errand_deposit')));
            if($update_array['o2o_errand_deposit']==0){
                $this->error('保证金必须大于0');
            }
            $result = $config_model->editConfig($update_array);
            if ($result) {
                dkcache('config');
                $this->log(lang('ds_edit') . lang('o2o_errand_other'), 1);
                $this->success(lang('ds_common_save_succ'), 'O2oErrand/other');
            } else {
                $this->log(lang('ds_edit') . lang('o2o_errand_other'), 0);
            }
        }
    }
    public function distance_price() {
        $config_model = model('config');
        if (!request()->isPost()) {
            $list_config = rkcache('config', true);
            $list_config['o2o_errand_distance_price']= unserialize($list_config['o2o_errand_distance_price']);
            View::assign('list_config', $list_config['o2o_errand_distance_price']);
            $this->setAdminCurItem('distance_price');
            return View::fetch();
        } else {
            $update_array=array();
            $update_array['o2o_errand_distance_price'] = $this->formatO2oErrandDistancePrice();
            $result = $config_model->editConfig($update_array);
            if ($result) {
                dkcache('config');
                $this->log(lang('ds_edit') . lang('o2o_errand_distance_price'), 1);
                $this->success(lang('ds_common_save_succ'), 'O2oErrand/distance_price');
            } else {
                $this->log(lang('ds_edit') . lang('o2o_errand_distance_price'), 0);
            }
        }
    }
    
    public function weight_price() {
        $config_model = model('config');
        if (!request()->isPost()) {
            $list_config = rkcache('config', true);
            $list_config['o2o_errand_weight_price']= unserialize($list_config['o2o_errand_weight_price']);
            View::assign('list_config', $list_config['o2o_errand_weight_price']);
            $this->setAdminCurItem('weight_price');
            return View::fetch();
        } else {
            $update_array=array();
            $update_array['o2o_errand_weight_price'] = $this->formatO2oErrandWeightPrice();
            $result = $config_model->editConfig($update_array);
            if ($result) {
                dkcache('config');
                $this->log(lang('ds_edit') . lang('o2o_errand_weight_price'), 1);
                $this->success(lang('ds_common_save_succ'), 'O2oErrand/weight_price');
            } else {
                $this->log(lang('ds_edit') . lang('o2o_errand_weight_price'), 0);
            }
        }
    }
    
    public function time_price() {
        $config_model = model('config');
        if (!request()->isPost()) {
            $list_config = rkcache('config', true);
            $list_config['o2o_errand_time_price']= unserialize($list_config['o2o_errand_time_price']);
            View::assign('list_config', $list_config['o2o_errand_time_price']);
            $this->setAdminCurItem('time_price');
            return View::fetch();
        } else {
            $update_array=array();
            $update_array['o2o_errand_time_price'] = $this->formatO2oErrandTimePrice();
            $result = $config_model->editConfig($update_array);
            if ($result) {
                dkcache('config');
                $this->log(lang('ds_edit') . lang('o2o_errand_time_price'), 1);
                $this->success(lang('ds_common_save_succ'), 'O2oErrand/time_price');
            } else {
                $this->log(lang('ds_edit') . lang('o2o_errand_time_price'), 0);
            }
        }
    }
    
    
    
    private function formatO2oErrandDistancePrice() {
        $o2o_errand_distance_price = input('param.o2o_errand_distance_price/a');
        if (!$o2o_errand_distance_price) {
            $this->error(lang('o2o_errand_distance_price_require'));
        }
        $o2o_errand_distance_price = array_values($o2o_errand_distance_price);
        $len = count($o2o_errand_distance_price);
        if($len==1){
          //默认起始值
          $j=0;
          $o2o_errand_distance_price[$j]=$this->getDistanceItem($o2o_errand_distance_price[$j],0);
        }
        //冒泡排序法，按照从小到大排序
        for ($i = 1; $i < $len; $i++) {
            for ($j = 0; $j < $len - $i; $j++) {
                $o2o_errand_distance_price[$j]=$this->getDistanceItem($o2o_errand_distance_price[$j],($j>0 && isset($o2o_errand_distance_price[$j-1]['end_distance']))?$o2o_errand_distance_price[$j-1]['end_distance']:0);
                $o2o_errand_distance_price[$j+1]=$this->getDistanceItem($o2o_errand_distance_price[$j+1],$o2o_errand_distance_price[$j]['end_distance']);
             
                if ($o2o_errand_distance_price[$j]['end_distance'] > $o2o_errand_distance_price[$j + 1]['end_distance']) {
                    $temp = $o2o_errand_distance_price[$j + 1];
                    $o2o_errand_distance_price[$j + 1] = $o2o_errand_distance_price[$j];
                    $o2o_errand_distance_price[$j] = $temp;
                }
//                if ($o2o_errand_distance_price[$j + 1]['start_distance'] < $o2o_errand_distance_price[$j]['end_distance']) {
//                    $this->error(sprintf(lang('o2o_errand_distance_content_error'),$o2o_errand_distance_price[$j]['title'],$o2o_errand_distance_price[$j+1]['title']));
//                }
            }
        }
        return serialize($o2o_errand_distance_price);
    }
    
    private function getDistanceItem($item, $start_distance) {
        $item['start_distance'] = $start_distance;
        $item['end_distance'] = abs(floatval($item['end_distance']));
        $item['interval_distance'] = abs(floatval($item['interval_distance']));
        $item['price'] = abs(floatval($item['price']));
        if (!$item['if_fixed'] && $item['interval_distance'] == 0) {
            $this->error('非固定费用时距离增量不能为0');
        }
        $item['title'] = $item['start_distance'] . '~' . $item['end_distance'] . lang('o2o_errand_kilometre');
        $item['content'] = $item['if_fixed'] ? ($item['price'] . lang('ds_yuan')) : sprintf(lang('o2o_errand_distance_content'), $item['interval_distance'], $item['price']);
        return $item;
    }
    
    
    private function formatO2oErrandWeightPrice() {
        $o2o_errand_weight_price = input('param.o2o_errand_weight_price/a');
        if (!$o2o_errand_weight_price) {
            $this->error(lang('o2o_errand_weight_price_require'));
        }
        $o2o_errand_weight_price = array_values($o2o_errand_weight_price);
        $len = count($o2o_errand_weight_price);
        if($len==1){
          //默认起始值
          $j=0;
          $o2o_errand_weight_price[$j]=$this->getWeightItem($o2o_errand_weight_price[$j],0);
        }
        //冒泡排序法，按照从小到大排序
        for ($i = 1; $i < $len; $i++) {
            for ($j = 0; $j < $len - $i; $j++) {
                $o2o_errand_weight_price[$j]=$this->getWeightItem($o2o_errand_weight_price[$j],($j>0 && isset($o2o_errand_weight_price[$j-1]['end_weight']))?$o2o_errand_weight_price[$j-1]['end_weight']:0);
                $o2o_errand_weight_price[$j+1]=$this->getWeightItem($o2o_errand_weight_price[$j+1],$o2o_errand_weight_price[$j]['end_weight']);

                if ($o2o_errand_weight_price[$j]['start_weight'] > $o2o_errand_weight_price[$j + 1]['start_weight']) {
                    $temp = $o2o_errand_weight_price[$j + 1];
                    $o2o_errand_weight_price[$j + 1] = $o2o_errand_weight_price[$j];
                    $o2o_errand_weight_price[$j] = $temp;
                }
                
//                if ($o2o_errand_weight_price[$j + 1]['start_weight'] < $o2o_errand_weight_price[$j]['end_weight']) {
//                    $this->error(sprintf(lang('o2o_errand_weight_content_error'),$o2o_errand_weight_price[$j]['title'],$o2o_errand_weight_price[$j+1]['title']));
//                }
            }
        }
        return serialize($o2o_errand_weight_price);
    }
    
    private function getWeightItem($item, $start_weight) {
        $item['start_weight'] = $start_weight;
        $item['end_weight'] = abs(floatval($item['end_weight']));
        $item['interval_weight'] = abs(floatval($item['interval_weight']));
        $item['price'] = abs(floatval($item['price']));
        if (!$item['if_fixed'] && $item['interval_weight'] == 0) {
            $this->error('非固定费用时重量增量不能为0');
        }
        $item['title'] = $item['start_weight'] . '~' . $item['end_weight'] . lang('o2o_errand_kilogram');
        $item['content'] = $item['if_fixed'] ? ($item['price'] . lang('ds_yuan')) : sprintf(lang('o2o_errand_weight_content'), $item['interval_weight'], $item['price']);
        return $item;
    }

    private function formatO2oErrandTimePrice() {
        $o2o_errand_time_price = input('param.o2o_errand_time_price/a');
        if (!$o2o_errand_time_price) {
            $this->error(lang('o2o_errand_time_price_require'));
        }
        $o2o_errand_time_price = array_values($o2o_errand_time_price);
        $len = count($o2o_errand_time_price);
        if($len==1){
          //默认起始值
          $j=0;
          $o2o_errand_time_price[$j]=$this->getPriceItem($o2o_errand_time_price[$j]);
        }
        //冒泡排序法，按照从小到大排序
        for ($i = 1; $i < $len; $i++) {
            for ($j = 0; $j < $len - $i; $j++) {

                $o2o_errand_time_price[$j]=$this->getPriceItem($o2o_errand_time_price[$j]);
                $o2o_errand_time_price[$j+1]=$this->getPriceItem($o2o_errand_time_price[$j+1]);

                if ($o2o_errand_time_price[$j]['start_time'] > $o2o_errand_time_price[$j + 1]['start_time']) {
                    $temp = $o2o_errand_time_price[$j + 1];
                    $o2o_errand_time_price[$j + 1] = $o2o_errand_time_price[$j];
                    $o2o_errand_time_price[$j] = $temp;
                }
                if ($o2o_errand_time_price[$j + 1]['start_time'] < $o2o_errand_time_price[$j]['end_time']) {
                    $this->error(sprintf(lang('o2o_errand_time_content_error'),$o2o_errand_time_price[$j]['title'],$o2o_errand_time_price[$j+1]['title']));
                }
            }
        }
        return serialize($o2o_errand_time_price);
    }
    
    private function getPriceItem($item) {
        if (isset($item['time'])) {
            $temp = explode(',', $item['time']);
            unset($item['time']);
            $item['start_time'] = $temp[0];
            $item['end_time'] = $temp[1];
        }
        $item['price'] = abs(floatval($item['price']));
        $temp = $item['start_time'];
        if ($temp > 1440) {
            $this->error(lang('o2o_errand_time_start_error'));
        }
        $start_time = (($temp < 600) ? '0' : '') . ($temp / 60) . ':00';
        $temp = $item['end_time'];
        $end_time = (($temp < 600) ? '0' : (($temp >= 1440) ? '次日0' : '')) . (($temp / 60) % 24) . ':00';
        $item['title'] = $start_time . '~' . $end_time;
        $item['content'] = $item['price'] . lang('ds_yuan');
        return $item;
    }

    protected function getAdminItemList() {
        $menu_array = array(
            array(
                'name' => 'distance_price',
                'text' => lang('o2o_errand_distance_price'),
                'url' => url('O2oErrand/distance_price')
            ),
            array(
                'name' => 'weight_price',
                'text' => lang('o2o_errand_weight_price'),
                'url' => url('O2oErrand/weight_price')
            ),
            array(
                'name' => 'time_price',
                'text' => lang('o2o_errand_time_price'),
                'url' => url('O2oErrand/time_price')
            ),
            array(
                'name' => 'other',
                'text' => lang('o2o_errand_other'),
                'url' => url('O2oErrand/other')
            ),
        );
        return $menu_array;
    }

}
